Imports System.Drawing
Public Class ColorMatrix
    Implements IDisposable
    'NullColor is Black
    Public data(,,) As Color
    Public x, y, z As Integer
    Public Sub New()
        ReDim data(0, 0, 0)
        data(0, 0, 0) = Color.Black
        x = 1
        y = 1
        z = 1
    End Sub
    Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer)
        Me.x = x
        Me.y = y
        Me.z = z
        ReDim data(0 To x - 1, 0 To y - 1, 0 To z - 1)
        For i As Integer = 0 To x - 1
            For j As Integer = 0 To y - 1
                For k As Integer = 0 To z - 1
                    data(i, j, k) = Color.Black
                Next
            Next
        Next
    End Sub
    Public Sub New(ByVal data(,,) As Color, ByVal i As Integer, ByVal j As Integer, ByVal k As Integer)
        Me.data = data
        Me.x = i
        Me.y = j
        Me.z = k
    End Sub
    Private Disposed As Boolean = False
    Private Overloads Sub Dispose(ByVal disposing As Boolean)
        ' Check to see if Dispose has already been called.
        If Not Me.Disposed Then
            ' If disposing equals true, dispose all managed 
            ' and unmanaged resources.
            If disposing Then
                ' Dispose managed resources.
                Me.Dispose()
            End If

            ' Call the appropriate methods to clean up 
            ' unmanaged resources here.
            ' If disposing is false, 
            ' only the following code is executed.
            x = 0
            y = 0
            z = 0
            ReDim data(0, 0, 0)
        End If
        Disposed = True
    End Sub
    ' Implement IDisposable.
    ' Do not make this method virtual.
    ' A derived class should not be able to override this method.
    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        ' This object will be cleaned up by the Dispose method.
        ' Therefore, you should call GC.SupressFinalize to
        ' take this object off the finalization queue 
        ' and prevent finalization code for this object
        ' from executing a second time.
        GC.SuppressFinalize(Me)
    End Sub
    Function TransverseSlice(ByVal zslice As Integer) As Bitmap
        Dim ans As New Bitmap(Me.x, Me.y)
        Dim i, j As Integer
        For i = 0 To Me.x - 1
            For j = 0 To Me.y - 1
                ans.SetPixel(i, j, Me.data(i, j, zslice))
            Next
        Next
        Return ans
    End Function
    Function CoronalSlice(ByVal yslice As Integer) As Bitmap
        Dim ans As New Bitmap(Me.x, Me.z)
        Dim i, j As Integer
        For i = 0 To Me.x - 1
            For j = 0 To Me.z - 1
                ans.SetPixel(i, Me.z - 1 - j, Me.data(i, yslice, j))
            Next
        Next
        Return ans
    End Function
    Function SagittalSlice(ByVal xslice As Integer) As Bitmap
        Dim ans As New Bitmap(Me.y, Me.z)
        Dim i, j As Integer
        For i = 0 To Me.y - 1
            For j = 0 To Me.z - 1
                ans.SetPixel(Me.y - 1 - i, Me.z - 1 - j, Me.data(xslice, i, j))
            Next
        Next
        Return ans
    End Function
End Class